using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FightAction : MonoBehaviour
{
	public GameObject bloodBar;
	public GameObject effectTip;
	public GameObject bloodTip;
	public GameObject momentumBar;
	ClipAnimation mClipAni;
	float maxBloodBarLength;
	float maxMomentumBarLength;
	Vector3 topPoint;
	EffectManagement effect = EffectManagement.GetInstance ();
	Vector3 bloodTipOriginalPosition;
	Vector3 bloodTipOriginalScale;
	Vector3 effectTipOriginalPosition;
	Vector3 effectTipOriginalScale;
	bool nearAttack = false;
	Vector3 originalPos;
	Transform mTrans;
	//攻击的发起者//
	GameObject attackInitiator;
	public List<DamageVo> damageVos;
	DamageVo currentDamage;
	GameObject mTarget;
	string mSkill;
	GameObject[] mTargets = null;
	
	public Vector3 TopPoint {
		get {
			return topPoint;
		}
		set {
			topPoint = value;
		}
	}
	
	public ClipAnimation ClipAni {
		get {
			if (mClipAni == null) {
				mClipAni = GetComponentInChildren<ClipAnimation> ();
			}
			return mClipAni;
		}
		set {
			mClipAni = value;
		}
	}
	
	public GameObject AttackInitiator {
		get {
			return attackInitiator;
		}set {
			attackInitiator = value;
		}
	}
	
	// Use this for initialization
	void Start ()
	{
		mTrans = transform;
		
		mClipAni = GetComponentInChildren<ClipAnimation> ();
		
		if (mClipAni == null) {
			mClipAni = gameObject.AddComponent<ClipAnimation> ();
		}
		
		if (bloodBar != null) {
			maxBloodBarLength = bloodBar.transform.localScale.x;
			
			bloodBar.transform.parent.localPosition = topPoint + Vector3.up * 20 + Vector3.left * maxBloodBarLength / 2;
		}
		
		if (momentumBar != null) {
			maxMomentumBarLength = momentumBar.transform.localScale.x;
		}
		
		originalPos = mTrans.position;
		
		if (bloodTip != null) {
			
			bloodTip.transform.localPosition = topPoint + Vector3.up * 30;
			
			bloodTipOriginalPosition = bloodTip.transform.position;
			bloodTipOriginalScale = bloodTip.transform.localScale;
			
			bloodTip.GetComponent<UILabel> ().enabled = false;
		}
		
		if (effectTip != null) {
			
			if (mTrans.localPosition.x > 0) {
				effectTip.transform.localPosition = topPoint + Vector3.up * 40 + Vector3.left * 100;
			} else {
				effectTip.transform.localPosition = topPoint + Vector3.up * 40 + Vector3.right * 100;
			}
			
			effectTipOriginalPosition = effectTip.transform.position;
			effectTipOriginalScale = effectTip.transform.localScale;
			
			effectTip.GetComponent<UISprite> ().enabled = false;
		}
	}
	
	/// <summary>
	/// 普通近程单体攻击.
	/// </summary>
	public void SingleNormalNearAttack (GameObject target, string skillName)
	{
		Debug.Log (gameObject.tag + "SingleNormalNearAttack ==>");
		Vector3 distance = target.transform.position - mTrans.position;
		distance = Vector3.Normalize (distance);
		Vector3 targetPoint = target.transform.position - distance * 0.35f;
		
		iTween.MoveTo (gameObject, iTween.Hash ("position", targetPoint, "time", 0.2f, "oncomplete", "MoveToEndNormal", 
			"oncompletetarget", gameObject));
		
		nearAttack = true;
		
		mTarget = target;
		mSkill = skillName;
	}
	
	/// <summary>
	/// 普通远程单体攻击.
	/// </summary>
	public void DirectNormalAttack (GameObject target, string skillName)
	{
		mClipAni.Play (GameDefine.NORMALACK_ANISTR, ClipAnimation.SwapMode.Once);
		
		nearAttack = false;
		effect.PlaySkillEffect (mTrans.position, target.transform.position, skillName);
		effect.AddEffectCallback (skillName, new GameObject[]{target}, damageVos.ToArray (), gameObject);
	}
	
	/// <summary>
	/// 技能远程单体攻击.
	/// </summary>
	public void DirectSkillAttack (GameObject target, string skillName)
	{
		Debug.Log (gameObject.tag + "DirectSkillAttack ==>");
		
		mClipAni.Play (GameDefine.SKILLACK_ANISTR, ClipAnimation.SwapMode.Once);
		
		nearAttack = false;
		effect.PlaySkillEffect (mTrans.position, target.transform.position, skillName);
		effect.AddEffectCallback (skillName, new GameObject[]{target}, damageVos.ToArray (), gameObject);
	}
	
	/// <summary>
	/// 技能远程群体攻击.
	/// </summary>
	public void GroupFarSkillAttack (GameObject[] targetObjs, string skillName)
	{
		mClipAni.Play (GameDefine.SKILLACK_ANISTR, ClipAnimation.SwapMode.Once);
		
		nearAttack = false;
		
		Vector3 otherGroupPoint = Vector3.zero;
		for (int i = 0; i < targetObjs.Length; i++) {
			otherGroupPoint += targetObjs [i].transform.position;
		}
		
		otherGroupPoint /= targetObjs.Length;
		
		effect.PlaySkillEffect (mTrans.position, otherGroupPoint, skillName);
		effect.AddEffectCallback (skillName, targetObjs, damageVos.ToArray (), gameObject);
	}
	/// <summary>
	/// 技能近程单体攻击.
	/// </summary>
	public void SingleNearSkillAttack (GameObject target, string skillName)
	{
		Vector3 distance = target.transform.position - mTrans.position;
		distance = Vector3.Normalize (distance);
		Vector3 targetPoint = target.transform.position - distance * 0.35f;
		
		iTween.MoveTo (gameObject, iTween.Hash ("position", targetPoint, "time", 0.2f, "oncomplete", "MoveToEndSkill", 
			"oncompletetarget", gameObject));
		
		nearAttack = true;
		
		mTarget = target;
		mSkill = skillName;
	}
	
	/// <summary>
	/// 技能近程群体攻击.
	/// </summary>
	public void GroupNearSkillAttack (GameObject[] targetObjs, string skillName)
	{
		Vector3 distance = targetObjs [0].transform.position - mTrans.position;
		distance = Vector3.Normalize (distance);
		Vector3 targetPoint = targetObjs [0].transform.position - distance * 0.35f;
		
		iTween.MoveTo (gameObject, iTween.Hash ("position", targetPoint, "time", 0.2f, "oncomplete", "MoveToEndGroup", 
			"oncompletetarget", gameObject));
		
		nearAttack = true;
		
		mTargets = targetObjs;
		mSkill = skillName;
	}
	
	void MoveToEndNormal ()
	{
		Debug.Log (gameObject.tag + "MoveToEndNormal ==>");
		
		mClipAni.Play (GameDefine.NORMALACK_ANISTR, ClipAnimation.SwapMode.Once);
		
		effect.PlaySkillEffect (mTrans.position, mTarget.transform.position, mSkill);
		effect.AddEffectCallback (mSkill, new GameObject[]{mTarget}, damageVos.ToArray (), gameObject);
		
		mTarget = null;
		mSkill = null;
	}
	
	void MoveToEndSkill ()
	{
		mClipAni.Play (GameDefine.SKILLACK_ANISTR, ClipAnimation.SwapMode.Once);
		
		effect.PlaySkillEffect (mTrans.position, mTarget.transform.position, mSkill);
		effect.AddEffectCallback (mSkill, new GameObject[]{mTarget}, damageVos.ToArray (), gameObject);
		
		mTarget = null;
		mSkill = null;
	}
	
	void MoveToEndGroup ()
	{
		mClipAni.Play (GameDefine.SKILLACK_ANISTR, ClipAnimation.SwapMode.Once);
		
		Vector3 otherGroupPoint = Vector3.zero;
		for (int i = 0; i < mTargets.Length; i++) {
			otherGroupPoint += mTargets [i].transform.position;
		}
		
		otherGroupPoint /= mTargets.Length;
		
		effect.PlaySkillEffect (mTrans.position, otherGroupPoint, mSkill);
		effect.AddEffectCallback (mSkill, mTargets, damageVos.ToArray (), gameObject);
		
		mTargets = null;
		mSkill = null;
	}
	
	public void beAttack (DamageVo damage)
	{
		Debug.Log (gameObject.tag + "beAttack ==>");
		
		if (damage.result != (int)GameDefine.FightInfoAttRet.FINGHTINFO_ACKRET_EVADE) {
			mClipAni.Play (GameDefine.BEATTACK_ANISTR, ClipAnimation.SwapMode.Once);
		}
		
		PlayEffectTip (damage.damageHp, damage.result);
		
		PlayBloodTip (damage.damageHp);
		
		ResetHp (damage.hp, damage.maxhp);
		
		currentDamage = damage;
	}
	
	void PlayEffectTip (int damageHp, int result)
	{
		if (result == (int)GameDefine.FightInfoAttRet.FINGHTINFO_ACKRET_ATTACKE)
			return;
		
		string effectStr = "";
		
		if (result == (int)GameDefine.FightInfoAttRet.FINGHTINFO_ACKRET_EVADE) {
			//闪避//
			effectStr = "Evade";
		} else if (result == (int)GameDefine.FightInfoAttRet.FINGHTINFO_ACKRET_CRIT) {
			//暴击//
			effectStr = "Crit";
		} else if (result == (int)GameDefine.FightInfoAttRet.FINGHTINFO_ACKRET_BLOCK) {
			//格挡//
			effectStr = "Block";
			
		}
		
		UISprite sprite = effectTip.GetComponent<UISprite> ();
		sprite.enabled = true;
		sprite.spriteName = effectStr;
		sprite.MakePixelPerfect ();
		
		effectTip.transform.position = effectTipOriginalPosition;
		effectTip.transform.localScale = effectTipOriginalScale;
		
		Vector3 targetPosition = effectTip.transform.position;
		targetPosition += Vector3.up * 0.15f;
		Vector3 targetScale = effectTip.transform.localScale * 1.5f;
		iTween.MoveTo (effectTip, iTween.Hash ("position", targetPosition, "time", 0.8f));
		
		if (damageHp != 0) {
			iTween.ScaleTo (effectTip, iTween.Hash ("scale", targetScale, "time", 0.8f, "easetype", "easeOutQuint"));
		} else {
			iTween.ScaleTo (effectTip, iTween.Hash ("scale", targetScale, "time", 0.8f, "easetype", "easeOutQuint", 
				"oncomplete", "BeAttackEnd", 
			"oncompletetarget", gameObject));
		}
	}
	
	void PlayBloodTip (int damageHp)
	{
		if (damageHp == 0) {
			return;
		}
		
		UILabel bloodLabel = bloodTip.GetComponent<UILabel> ();
		bloodLabel.enabled = true;
		bloodLabel.text = damageHp.ToString ();
		bloodLabel.MakePixelPerfect ();
		
		bloodTip.transform.position = bloodTipOriginalPosition;
		bloodTip.transform.localScale = bloodTipOriginalScale;
		
		Vector3 targetLabelPosition = bloodTip.transform.position;
		targetLabelPosition += Vector3.up * 0.15f;
		Vector3 targetScale = bloodTip.transform.localScale * 1.5f;
		
		iTween.MoveTo (bloodTip, iTween.Hash ("position", targetLabelPosition, "time", 0.8f));
		iTween.ScaleTo (bloodTip, iTween.Hash ("scale", targetScale, "easetype", "easeOutQuint", "time", 0.8f,
			"oncomplete", "BeAttackEnd", 
			"oncompletetarget", gameObject));
	}
	
	/// <summary>
	/// 重置血量.
	/// </summary>
	public void ResetHp (int hpVal, int maxHp)
	{
		if (bloodBar == null) {
			return;
		}
		
		if (hpVal != maxHp) {
			Vector3 bloodScale = bloodBar.transform.localScale;
			bloodScale.x = maxBloodBarLength * hpVal / maxHp;
		
			if (bloodScale.x == 0f) {
				bloodScale.x = 0.001f;
				bloodBar.GetComponent<UISprite> ().enabled = false;
			} else {
				bloodBar.transform.localScale = bloodScale;
			}
		}
	}
	
	/// <summary>
	/// 重置气势.
	public void ResetMomentum (int momentumVal, int maxMomentum)
	{
		if (momentumBar == null) {
			return;
		}
		
		if (momentumVal != maxMomentum) {
		
			Vector3 maxMomentumScale = momentumBar.transform.localScale;
			maxMomentumScale.x = maxMomentumBarLength * momentumVal / maxMomentum;
		
			if (maxMomentumScale.x == 0f) {
				maxMomentumScale.x = 0.001f;
				momentumBar.GetComponent<UISprite> ().enabled = false;
			} else {
				momentumBar.transform.localScale = maxMomentumScale;
			}
		}
	}
	
	/// <summary>
	/// 显示buf.
	/// </summary>
	public void ShowBuffer (string buffer)
	{
		
	}
	
	void BeAttackEnd ()
	{
		Debug.Log (gameObject.tag + "BeAttackEnd ==>");
		
		UILabel label = bloodTip.GetComponent<UILabel> ();
		label.enabled = false;
		
		UISprite sprite = effectTip.GetComponent<UISprite> ();
		sprite.enabled = false;
		
		attackInitiator.SendMessage ("PlayNextFlow", SendMessageOptions.DontRequireReceiver);
		
		DeadCheck ();
	}
	
	void DeadCheck ()
	{
		if (currentDamage.state == (int)GameDefine.BattleState.BATTLE_STATE_DEAD) {
			gameObject.SetActive (false);
			
			EncounterToFight.GetInstance ().RemoveObj (gameObject);
			
			DestroyImmediate (gameObject);
		}
	}
	
	void PlayNextFlow ()
	{
		Debug.Log (gameObject.tag + "PlayNextFlow ==>");
		if (nearAttack == true) {
			iTween.MoveTo (gameObject, iTween.Hash ("position", originalPos, "time", 0.2f, "oncomplete", "PlayNextFlow", "oncompletetarget", gameObject));
			nearAttack = false;
		} else {
			EncounterToFight.GetInstance ().nextFight ();
		}
	}
}
